package org.socialresume.client.mvp.presenter;

import java.util.List;

import net.customware.gwt.dispatch.client.DispatchAsync;

import org.enunes.gwt.mvp.client.EventBus;
import org.enunes.gwt.mvp.client.presenter.BasePresenter;
import org.socialresume.client.model.Feed;
import org.socialresume.client.model.FeedItem;
import org.socialresume.client.model.FeedTypeEnum;
import org.socialresume.client.model.TwitterFeed;
import org.socialresume.client.model.action.GetUpdates;
import org.socialresume.client.model.action.GetUpdatesResult;
import org.socialresume.client.mvp.presenter.FeedPresenter.Display;
import org.socialresume.client.service.GreetingService;
import org.socialresume.client.service.GreetingServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;

public class FeedPresenterImpl extends BasePresenter<Display> implements
		FeedPresenter {

	protected final GreetingServiceAsync updateService = GWT
			.create(GreetingService.class);
	private Feed currentFeed;
	private DispatchAsync dispatcher;

	@Inject
	public FeedPresenterImpl(final DispatchAsync dispatcher, EventBus eventBus, Display display) {
		super(eventBus, display);
		this.dispatcher = dispatcher;
	}

	@Override
	public void bind() {
		super.bind();
	}

	@Override
	public void setFeed(final Feed feed) {
		if (currentFeed == null || !feed.getUrl().equals(currentFeed.getUrl())) {
			currentFeed = feed;
			display.getLoadingVisibility().setValue(true);
			if (FeedTypeEnum.TWITTER.equals(feed.getType())) {
				TwitterFeed twitterFeed = (TwitterFeed) feed;
				dispatcher.<GetUpdates,GetUpdatesResult>execute(new GetUpdates(twitterFeed.getTwitterLogin(),null), new AsyncCallback<GetUpdatesResult>() {
					@Override
					public void onFailure(Throwable caught) {
						display.getLoadingVisibility().setValue(false);
						setItems(null);
						GWT.log("resut not found for feed", caught);
					}
	
					@Override
					public void onSuccess(GetUpdatesResult result) {
						display.getLoadingVisibility().setValue(false);
						setItems(result.getItems());
						GWT.log("resut found for feed", null);
					}
				});
			}
//			if (FeedTypeEnum.TWITTER.equals(feed.getType())) {
//				GWT.log("Twitter update", null);
//				TwitterFeed twitterFeed = (TwitterFeed) feed;
//				updateService.getTwitterUpdates(twitterFeed.getTwitterLogin(),
//						null, new AsyncCallback<List<FeedItem>>() {
//
//							@Override
//							public void onFailure(Throwable caught) {
//								display.getLoadingVisibility().setValue(false);
//								setItems(null);
//							}
//
//							@Override
//							public void onSuccess(List<FeedItem> items) {
//								display.getLoadingVisibility().setValue(false);
//								setItems(items);
//							}
//						});
//			} else {
//				GWT.log("Feed update", null);
//				updateService.getUpdates(feed.getType(), feed.getUrl(),
//						new AsyncCallback<List<FeedItem>>() {
//							@Override
//							public void onSuccess(List<FeedItem> result) {
//								display.getLoadingVisibility().setValue(false);
//								setItems(null);
//							}
//
//							@Override
//							public void onFailure(Throwable caught) {
//								display.getLoadingVisibility().setValue(false);
//								setItems(null);
//							}
//						});
//			}
		}
	}

	public void setItems(List<FeedItem> items) {
		display.clear();
		if (items != null) {
			for (FeedItem item : items) {
				display.addFeed(display.getFeedItemWidget(currentFeed, item));
			}
		}
	}
}
